package com.wooribank.android.tablet.common;

import java.io.InputStream;
import java.util.ArrayList;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.wooribank.android.tablet.Account;
import com.wooribank.android.tablet.MyApplication;

public class JsonManager {
	private static JsonManager instance;

	private JsonManager() {

	}

	public static JsonManager getInstance() {
		if (instance == null) {
			instance = new JsonManager();
		}
		return instance;
	}

	private static InputStream getInputStream(String urlString) {
		return null;
	}

	public void getAccounts(String urlString, Handler handler) {
		GetDataAsyncTask asyncTask = new GetDataAsyncTask();
		asyncTask.execute(urlString, handler);
	}

	private static class GetDataAsyncTask extends AsyncTask<Object, Void, ArrayList<Account>> {
		private Handler handler;

		@Override
		protected ArrayList<Account> doInBackground(Object... args) {
			String urlString = (String) args[0];
			try {
				Thread.currentThread().sleep(1000);
			} catch (InterruptedException ioe) {

			}
			handler = (Handler) args[1];
			return Account.parseAccounts(getInputStream(urlString));
		}

		@Override
		protected void onPostExecute(ArrayList<Account> result) {
			Message msg = handler.obtainMessage();
			Bundle bundle = new Bundle();

			bundle.putParcelableArrayList(MyApplication.KEY_GET_DATA, result);
			msg.setData(bundle);
			handler.sendMessage(msg);
		}
	}
}
